/*
 * @(#) IndexAction.java 2011-9-6
 *
 * Copyright (c) 2011, Strong Technology. All Rights Reserved.
 * Strong Technology. CONFIDENTIAL
 */
package cn.strong.gweva.common.presentation.action;

import java.util.Date;

import cn.strong.gweva.common.business.entity.UserPwdEntity;
import cn.strong.gweva.common.business.service.LoginService;
import cn.strong.gweva.common.util.StringUtil;
import cn.strong.gweva.common.util.security.Cryptographer;



/**
 * 后台管理首页Action
 * 
 * @author lilanfei
 * @version 1.0
 * @since 2011-9-6
 */
public class IndexAction extends AjaxBaseAction
{

	/** serialVersionUID */
	private static final long serialVersionUID = 5760464117641467312L;
	
	/** 服务 */
	private LoginService loginService;
	
	/** 密码修改 */
	private UserPwdEntity pwdChange;

	/**
	 * 首页
	 * 
	 * @return
	 * @throws Exception
	 */
	public String excute() throws Exception
	{
		try
		{
			return SUCCESS;
		}
		catch (Exception e)
		{
			return processException(e);
		}
	}
	
	/**
	 * 密码修改页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String changePwdInit() throws Exception
	{
		try
		{
			return SUCCESS;
		}
		catch (Exception e)
		{
			return processException(e);
		}
	}
	
	/**
	 * 校验旧密码
	 * 
	 * @return
	 * @throws Exception
	 */
	public String checkOldPwd() throws Exception
	{
		try
		{
			UserPwdEntity userPwd = new UserPwdEntity();
			userPwd.setUserId(this.getLoginUser().getUserId());
			userPwd.setOldPwd(Cryptographer.encodeToMD5(pwdChange.getOldPwd()));
			writeJson(loginService.checkOldPwd(userPwd));
			
			return SUCCESS;
		}
		catch (Exception e)
		{
			return processException(e);
		}
	}
	
	/**
	 * 修改密码
	 * 
	 * @return
	 * @throws Exception
	 */
	public String changePwd() throws Exception
	{
		try
		{
			UserPwdEntity userPwd = new UserPwdEntity();
			userPwd.setUserId(this.getLoginUser().getUserId());
			userPwd.setOldPwd(Cryptographer.encodeToMD5(pwdChange.getOldPwd()));
			if (!StringUtil.isEmpty(pwdChange.getNewPwd()))
			{
				userPwd.setNewPwd(Cryptographer.encodeToMD5(pwdChange.getNewPwd()));
			}
			userPwd.setUpdateTime(new Date());
			userPwd.setUpdateUserId(this.getLoginUser().getUserId());
			int cnt = loginService.changePwd(userPwd);
			this.setPwdChange(null);
			
			if (cnt > 0)
			{
				this.setIsSuccess("密码修改成功！");
			}
			else
			{
				this.setIsSuccess("密码修改失败！");
			}
			
			return SUCCESS;
		}
		catch (Exception e)
		{
			return processException(e);
		}
	}

	/**
	 * 获取服务
	 * @return loginService 服务
	 */
	public LoginService getLoginService()
	{
		return loginService;
	}
	/**
	 * 设定服务
	 * @param loginService 服务
	 */
	public void setLoginService(LoginService loginService)
	{
		this.loginService = loginService;
	}

	/**
	 * 获取
	 * @return pwdChange
	 */
	public UserPwdEntity getPwdChange()
	{
		return pwdChange;
	}
	/**
	 * 设定
	 * @param pwdChange 
	 */
	public void setPwdChange(UserPwdEntity pwdChange)
	{
		this.pwdChange = pwdChange;
	}
}
